/**加载比较长且对时间要求不高的情况的音乐文件
 * data 2011 5 11
 * author @zzy
 */
package cn.edu.dlut.WingGame.Sound;

import java.io.IOException;

import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;
import cn.edu.dlut.WingGame.Game.WingGame;

public class WingMusic {

	protected static MediaPlayer mediaPlayer;
	protected static boolean mustResume = false;

	private static void prepareMediaPlayer() {
		if (mediaPlayer == null) {
			mediaPlayer = new MediaPlayer();
		}
	}

	// 播放assets中的一个音乐文件
	public static void play(String file, boolean loop) {

		prepareMediaPlayer();
		mediaPlayer.setLooping(loop);
		AssetFileDescriptor afd = null;

		try {
			afd = WingGame.getActivity().getAssets().openFd(file);
		} catch (Exception e) {
			e.printStackTrace();
			Log.v("Sound", "can't be read!");
			return;
		}

		try {
			mediaPlayer.reset();
			mediaPlayer.setDataSource(afd.getFileDescriptor(), afd
					.getStartOffset(), afd.getLength());
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			afd.close();
		} catch (IOException e) {
			e.printStackTrace();
			Log.v("Sound", "can't be set data sourse!");
			return;
		} catch (IllegalStateException e) {
			e.printStackTrace();
			Log.v("Error", "setting data source in WingMusic.play, fail ...");
			return;
		}

		try {
			mediaPlayer.prepare();
		} catch (Exception e) {
			e.printStackTrace();
			Log.v("Error", " preparing MediaPlayer");
			return;
		}

		mediaPlayer.start();
	}

	// 播放assets中的一个音乐文件，没有循环
	public static void play(String file) {
		play(file, false);
	}

	// 播放assets中的一个音乐文件
	public static void playMuisc() {
		if (mediaPlayer == null)
			return;

		mediaPlayer.start();
	}

	// 用于外部调用定义一个MediaPlayer变量
	public static MediaPlayer creat(String file, boolean loop) {
		prepareMediaPlayer();
		mediaPlayer.setLooping(loop);
		AssetFileDescriptor afd = null;

		try {
			afd = WingGame.getActivity().getAssets().openFd(file);
		} catch (Exception e) {
			e.printStackTrace();
			Log.v("Sound", "can't be read!");
			return null;
		}

		try {
			mediaPlayer.reset();
			mediaPlayer.setDataSource(afd.getFileDescriptor(), afd
					.getStartOffset(), afd.getLength());
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			afd.close();
		} catch (IOException e) {
			e.printStackTrace();
			Log.v("Sound", "can't be set data sourse!");
			return null;
		} catch (IllegalStateException e) {
			e.printStackTrace();
			Log.v("Error", "setting data source in WingMusic.play, fail ...");
			return null;
		}

		try {
			mediaPlayer.prepare();
		} catch (Exception e) {
			e.printStackTrace();
			Log.v("Error", " preparing MediaPlayer");
			return null;
		}

		return mediaPlayer;
	}

	public static void play() {
		if (mediaPlayer == null)
			return;
		mediaPlayer.start();
	}

	public static void setVolume(float volume) {
		mediaPlayer.setVolume(volume, volume);
	}

	public static void stop() {
		if (mediaPlayer == null)
			return;
		mediaPlayer.stop();
	}

	public static void pause() {
		if (mediaPlayer == null)
			return;
		mediaPlayer.pause();
	}

	// 当activity暂停时调用
	public static void onPause() {
		if (mediaPlayer == null)
			return;

		if (mediaPlayer.isPlaying()) {
			mediaPlayer.pause();
			mustResume = true;
		}
	}

	// 当activity继续时调用
	public static void onResume() {
		if (mediaPlayer == null)
			return;

		if (mustResume) {
			mediaPlayer.start();
			mustResume = false;
		}
	}

	// 获取音乐流文件中的对象
	public static MediaPlayer getMediaPlayer() {
		return mediaPlayer;
	}

}
